import Test from "@/views/test.vue";
import { getLogin } from "@/api";
import { getToken } from "@/utils/token";
import HomePage from "@/views/HomePage.vue";
import AdminLogin from "@/views/admin/AdminLogin.vue";
import AdminDashboard from "@/views/admin/AdminDashboard.vue";
import Login from "@/views/Login.vue";
import Terms from "@/views/Terms.vue";
import Privacy from "@/views/Privacy.vue";
import Help from "@/views/Help.vue";
import { createRouter, createWebHistory } from "vue-router";
import Register from "@/views/Register.vue";
import ResetAccount from "@/views/ResetAccount.vue";
import ServiceCategory from "@/views/category/ServiceCategory.vue";
import CabinetRepair from "@/views/CabinetRepair.vue";
import CurtainInstallation from "@/views/CurtainInstallation.vue";
import PersonalCenter from "@/views/PersonalCenter.vue";
import IkeaAssemblyPage from "@/views/IkeaAssemblyPage.vue";
import EnterpriseServices from "@/views/EnterpriseServices.vue";
import UniversalCard from "@/views/UniversalCard.vue";
import ViewmorePage from "@/views/ViewmorePage.vue";
import Setting from "@/views/Setting.vue";
import ServiceSearch from "@/views/category/ServiceSearch.vue";
// import TestServiceRouting from "@/views/TestServiceRouting.vue";
// import Register from "@/views/Register.vue";

import WaitingQuote from "@/views/WaitingQuote.vue";
import CurtainOrderConfirm from "@/views/CurtainOrderConfirm.vue";
import ConfirmOrder from "@/views/ConfirmOrder.vue";
import InsurancePage from "@/views/InsurancePage.vue";

import OrderPage from "@/views/OrderPage.vue";
import ReviewDetails from "@/views/ReviewDetails.vue";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      redirect: "/home",
    },
    {
      name: "test",
      path: "/test",
      component: Test,
    },
    {
      name: "home",
      path: "/home",
      component: HomePage,
    },
    {
      name: "login",
      path: "/login",
      component: Login,
    },
    {
      path: "/terms",
      name: "terms",
      component: Terms,
    },
    {
      path: "/privacy",
      name: "privacy",
      component: Privacy,
    },
    {
      path: "/help",
      name: "help",
      component: Help,
    },
    {
      name: "register",
      path: "/register",
      component: Register,
    },
    {
      name: "reset",
      path: "/reset",
      component: ResetAccount,
    },
    {
      name: "center",
      path: "/center",
      component: PersonalCenter,
    },
    {
      name: "set",
      path: "/set",
      component: Setting,
    },
    {
      name: "admin-login",
      path: "/admin/login",
      component: AdminLogin,
    },
    {
      name: "admin-dashboard",
      path: "/admin/dashboard",
      component: AdminDashboard,
    },
    {
      name: "category",
      path: "/category",
      component: ServiceCategory,
    },
    {
      name: "cabinetrepair",
      path: "/cabinetrepair",
      component: CabinetRepair,
    },
    {
      name: "curtaininstallation",
      path: "/curtaininstallation",
      component: CurtainInstallation,
    },

    {
      name: "ikeaassembly",
      path: "/ikeaassembly",
      component: IkeaAssemblyPage,
    },
    {
      name: "enterpriseservices",
      path: "/enterpriseservices",
      component: EnterpriseServices,
    },
    {
      name: "universalcard",
      path: "/universalcard",
      component: UniversalCard,
    },
    {
      name: "viewmore",
      path: "/viewmore",
      component: ViewmorePage
    },

  {
    path: '/confirm-order',
    name: 'ConfirmOrderNew',
    component: ConfirmOrder
  },
  {
    path: '/insurance',
    name: 'InsurancePage',
    component: InsurancePage
  },
  {
    path: '/waiting-quote',
    name: 'WaitingQuote',
    component: WaitingQuote
  },
  {
    path: '/curtain-order-confirm',
    name: 'CurtainOrderConfirm',
    component: CurtainOrderConfirm
  },
  {
    path: '/orderPage',
    name: 'orderPage',
    component: OrderPage
  },
  {
    path: '/reviewDetails',
    name: 'reviewDetails',
    component: ReviewDetails
  }
  ],
});

//全局前置路由守卫
router.beforeEach((to, from, next) => {
  const token = getToken();
  if (token) {
    if (to.path == "/login" || to.path == "/reg") {
      // next({ path: "/" });
      next();
    } else {
      next();
    }
  } else {
    next();
  }
});

export default router;